package com.wan.controller;

import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user")
public class LoginController {

    @RequestMapping("/login")
    public ModelAndView loginPage(){
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }


    @RequestMapping("/login/error")
    public ModelAndView loginError(HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView("login");
        AuthenticationException authenticationException = (AuthenticationException) request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        if (authenticationException != null){
            if (authenticationException.toString().contains("BadCredentialsException")){
                modelAndView.addObject("errorMsg","用户名或密码错误");
            }
        }
        return modelAndView;
    }
}
